home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / dflat_r_.arc / HELPBOX.C < prev    next >
Text File  |  1991-10-02  |  17KB  |  661 lines

  1. /* ------------ helpbox.c ----------- */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <dos.h>
  7. #include <ctype.h>
  8. #include "dflat.h"
  9. #include "htree.h"
  10.  
  11. #ifdef INCLUDE_HELP
  12.  
  13. extern DBOX HelpBox;
  14.  
  15. /* -------- strings of D-Flat classes for calling default
  16.       help text collections -------- */
  17. char *ClassNames[] = {
  18.     #undef ClassDef
  19.     #define ClassDef(c,b,p,a) #c,
  20.     #include "classes.h"
  21.     NULL
  22. };
  23.  
  24. #define MAXHEIGHT (SCREENHEIGHT-10)
  25.  
  26. /* --------- linked list of help text collections -------- */
  27. struct helps {
  28.     char *hname;
  29.     char *NextName;
  30.     char *PrevName;
  31.     long hptr;
  32.     int bit;
  33.     int hheight;
  34.     int hwidth;
  35.     WINDOW hwnd;
  36.     struct helps *NextHelp;
  37. };
  38. static struct helps *FirstHelp;
  39. static struct helps *LastHelp;
  40. static struct helps *ThisHelp;
  41.  
  42. /* --- linked stack of help windows that have beed used --- */
  43. struct HelpStack {
  44.     char *hname;
  45.     struct HelpStack *PrevStack;
  46. };
  47. static struct HelpStack *LastStack;
  48. static struct HelpStack *ThisStack;
  49.  
  50. /* --- linked list of keywords in the current help
  51.            text collection (listhead is in window) -------- */
  52. struct keywords {
  53.     char *hname;
  54.     int lineno;
  55.     int off1, off2, off3;
  56.     int isDefinition;
  57.     struct keywords *nextword;
  58.     struct keywords *prevword;
  59. };
  60.  
  61. static FILE *helpfp;
  62. static char hline [160];
  63. static int Helping;
  64.  
  65. static void SelectHelp(WINDOW, char *);
  66. static void ReadHelp(WINDOW);
  67. static void FindHelp(char *);
  68. static void FindHelpWindow(WINDOW);
  69. static void DisplayDefinition(WINDOW, char *);
  70. static void BestFit(WINDOW, DIALOGWINDOW *);
  71.  
  72. int HelpBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  73. {
  74.     DBOX *db = wnd->extension;
  75.     WINDOW cwnd;
  76.     struct keywords *thisword;
  77.     static char HelpName[50];
  78.  
  79.     switch (msg)    {
  80.         case CREATE_WINDOW:
  81.             Helping = TRUE;
  82.             GetClass(wnd) = HELPBOX;
  83.             InitWindowColors(wnd);
  84.             if (ThisHelp != NULL)
  85.                 ThisHelp->hwnd = wnd;
  86.             break;
  87.         case INITIATE_DIALOG:
  88.             ReadHelp(wnd);
  89.             break;
  90.         case COMMAND:
  91.             switch ((int)p1)    {
  92.                 case ID_CANCEL:
  93.                     ThisStack = LastStack;
  94.                     while (ThisStack != NULL)    {
  95.                         LastStack = ThisStack->PrevStack;
  96.                         if (ThisStack->hname != NULL)
  97.                             free(ThisStack->hname);
  98.                         free(ThisStack);
  99.                         ThisStack = LastStack;
  100.                     }
  101.                     break;
  102.                 case ID_PREV:
  103.                     FindHelpWindow(wnd);
  104.                     if (ThisHelp != NULL)
  105.                         SelectHelp(wnd, ThisHelp->PrevName);
  106.                     return TRUE;
  107.                 case ID_NEXT:
  108.                     FindHelpWindow(wnd);
  109.                     if (ThisHelp != NULL)
  110.                         SelectHelp(wnd, ThisHelp->NextName);
  111.                     return TRUE;
  112.                 case ID_BACK:
  113.                     if (LastStack != NULL)    {
  114.                         if (LastStack->PrevStack != NULL)    {
  115.                             ThisStack = LastStack->PrevStack;
  116.                             if (LastStack->hname != NULL)
  117.                                 free(LastStack->hname);
  118.                             free(LastStack);
  119.                             LastStack = ThisStack;
  120.                             SelectHelp(wnd, ThisStack->hname);
  121.                         }
  122.                     }
  123.                     return TRUE;
  124.                 default:
  125.                     break;
  126.             }
  127.             break;
  128.         case KEYBOARD:
  129.             if (WindowMoving)
  130.                 break;
  131.             cwnd = ControlWindow(wnd->extension, ID_HELPTEXT);
  132.             if (cwnd == NULLWND || inFocus != cwnd)
  133.                 break;
  134.             thisword = cwnd->thisword;
  135.             switch ((int)p1)    {
  136.                 case '\r':
  137.                     if (thisword != NULL)    {
  138.                         if (thisword->isDefinition)
  139.                             DisplayDefinition(GetParent(wnd), thisword->hname);
  140.                         else    {
  141.                             strncpy(HelpName, thisword->hname,
  142.                                 sizeof HelpName);
  143.                             SelectHelp(wnd, HelpName);
  144.                         }
  145.                     }
  146.                     return TRUE;
  147.                 case '\t':
  148.                     if (thisword == NULL)
  149.                         thisword = cwnd->firstword;
  150.                     else {
  151.                         if (thisword->nextword == NULL)
  152.                             thisword = cwnd->firstword;
  153.                         else
  154.                             thisword = thisword->nextword;
  155.                     }
  156.                     break;
  157.                 case SHIFT_HT:
  158.                     if (thisword == NULL)
  159.                         thisword = cwnd->lastword;
  160.                     else {
  161.                         if (thisword->prevword == NULL)
  162.                             thisword = cwnd->lastword;
  163.                         else
  164.                             thisword = thisword->prevword;
  165.                     }
  166.                     break;
  167.                 default:
  168.                     thisword = NULL;
  169.                     break;
  170.             }
  171.             if (thisword != NULL)    {
  172.                 cwnd->thisword = thisword;
  173.                 if (thisword->lineno < cwnd->wtop ||
  174.                         thisword->lineno >= cwnd->wtop + ClientHeight(cwnd))    {
  175.                     int distance = ClientHeight(cwnd)/2;
  176.                     do    {
  177.                         cwnd->wtop = thisword->lineno - distance;
  178.                         distance /= 2;
  179.                     }
  180.                     while (cwnd->wtop < 0);
  181.                 }
  182.                 SendMessage(cwnd, PAINT, 0, 0);
  183.                 return TRUE;
  184.             }
  185.             break;
  186.         case CLOSE_WINDOW:
  187.             if (db != NULL)    {
  188.                 if (db->dwnd.title != NULL)    {
  189.                     free(db->dwnd.title);
  190.                     db->dwnd.title = NULL;
  191.                 }
  192.             }
  193.             FindHelpWindow(wnd);
  194.             if (ThisHelp != NULL)
  195.                 ThisHelp->hwnd = NULLWND;
  196.             Helping = FALSE;
  197.             break;
  198.         default:
  199.             break;
  200.     }
  201.     return BaseWndProc(HELPBOX, wnd, msg, p1, p2);
  202. }
  203.  
  204. static void SelectHelp(WINDOW wnd, char *hname)
  205. {
  206.     if (hname != NULL)    {
  207.         WINDOW pwnd = GetParent(wnd);
  208.         PostMessage(wnd, ENDDIALOG, 0, 0);
  209.         PostMessage(pwnd, DISPLAY_HELP, (PARAM) hname, 0);
  210.     }
  211. }
  212.  
  213. int HelpTextProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  214. {
  215.     struct keywords *thisword;
  216.     int rtn, mx, my;
  217.     switch (msg)    {
  218.         case PAINT:
  219.             if (wnd->thisword != NULL)    {
  220.                 WINDOW pwnd = GetParent(wnd);
  221.                 char *cp;
  222.                 thisword = wnd->thisword;
  223.                 cp = TextLine(wnd, thisword->lineno);
  224.                 cp += thisword->off1;
  225.                 *(cp+1) = (pwnd->WindowColors [SELECT_COLOR] [FG] & 255) | 0x80;
  226.                 *(cp+2) = (pwnd->WindowColors [SELECT_COLOR] [BG] & 255) | 0x80;
  227.                 rtn = DefaultWndProc(wnd, msg, p1, p2);
  228.                 *(cp+1) = (pwnd->WindowColors [HILITE_COLOR] [FG] & 255) | 0x80;
  229.                 *(cp+2) = (pwnd->WindowColors [HILITE_COLOR] [BG] & 255) | 0x80;
  230.                 return rtn;
  231.             }
  232.             break;
  233.         case LEFT_BUTTON:
  234.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  235.             mx = (int)p1 - GetClientLeft(wnd);
  236.             my = (int)p2 - GetClientTop(wnd);
  237.             my += wnd->wtop;
  238.             thisword = wnd->firstword;
  239.             while (thisword != NULL)    {
  240.                 if (my == thisword->lineno)    {
  241.                     if (mx >= thisword->off2 && mx < thisword->off3)    {
  242.                         wnd->thisword = thisword;
  243.                         SendMessage(wnd, PAINT, 0, 0);
  244.                         if (thisword->isDefinition)    {
  245.                             WINDOW pwnd = GetParent(wnd);
  246.                             if (pwnd != NULLWND)
  247.                                 DisplayDefinition(GetParent(pwnd),
  248.                                     thisword->hname);
  249.                         }
  250.                         break;
  251.                     }
  252.                 }
  253.                 thisword = thisword->nextword;
  254.             }
  255.             return rtn;
  256.         case DOUBLE_CLICK:
  257.             PostMessage(wnd, KEYBOARD, '\r', 0);
  258.             break;
  259.         case CLOSE_WINDOW:
  260.             thisword = wnd->firstword;
  261.             while (thisword != NULL)    {
  262.                 struct keywords *nextword = thisword->nextword;
  263.                 if (thisword->hname != NULL)
  264.                     free(thisword->hname);
  265.                 free(thisword);
  266.                 thisword = nextword;
  267.             }
  268.             break;
  269.         default:
  270.             break;
  271.     }
  272.     return DefaultWndProc(wnd, msg, p1, p2);
  273. }
  274.  
  275. static void *fgetshelp(void)
  276. {
  277.     void *fg;
  278.     do
  279.         if ((fg = GetHelpLine(hline)) == NULL)
  280.             break;
  281.     while (*hline == ';');    /* bypassing all comments */
  282.     return fg;
  283. }
  284.  
  285. static void ReadHelp(WINDOW wnd)
  286. {
  287.     WINDOW cwnd = ControlWindow(wnd->extension, ID_HELPTEXT);
  288.     int linectr = 0;
  289.     if (cwnd == NULLWND)
  290.         return;
  291.     cwnd->wndproc = HelpTextProc;
  292.     /* ----- read the help text ------- */
  293.     while (TRUE)    {
  294.         unsigned char *cp = hline, *cp1;
  295.         int colorct = 0;
  296.         if (fgetshelp() == NULL)
  297.             break;
  298.         if (*hline == '<')
  299.             break;
  300.         hline[strlen(hline)-1] = '\0';
  301.         /* --- add help text to the help window --- */
  302.         while (cp != NULL)    {
  303.             if ((cp = strchr(cp, '[')) != NULL)    {
  304.                 /* ----- hit a new key word ----- */
  305.                 struct keywords *thisword;
  306.                 if (*(cp+1) != '.' && *(cp+1) != '*')    {
  307.                     cp++;
  308.                     continue;
  309.                 }
  310.                 thisword = calloc(1, sizeof(struct keywords));
  311.                 if (thisword != NULL)    {
  312.                     if (cwnd->firstword == NULL)
  313.                         cwnd->firstword = thisword;
  314.                     if (cwnd->lastword != NULL)    {
  315.                         ((struct keywords *)
  316.                             (cwnd->lastword))->nextword = thisword;
  317.                         thisword->prevword = cwnd->lastword;
  318.                     }
  319.                     cwnd->lastword = thisword;
  320.                     thisword->lineno = cwnd->wlines;
  321.                     thisword->off1 = (int) (cp - hline);
  322.                     thisword->off2 = thisword->off1 - colorct * 4;
  323.                     thisword->isDefinition = *(cp+1) == '*';
  324.                 }
  325.                 colorct++;
  326.                 *cp++ = CHANGECOLOR;
  327.                 *cp++ = (wnd->WindowColors [HILITE_COLOR] [FG] & 255) | 0x80;
  328.                 *cp++ = (wnd->WindowColors [HILITE_COLOR] [BG] & 255) | 0x80;
  329.                 cp1 = cp;
  330.                 if ((cp = strchr(cp, ']')) != NULL)    {
  331.                     if (thisword != NULL)
  332.                         thisword->off3 = thisword->off2 + (int) (cp - cp1);
  333.                     *cp++ = RESETCOLOR;
  334.                 }
  335.                 if ((cp = strchr(cp, '<')) != NULL)    {
  336.                     char *cp1 = strchr(cp, '>');
  337.                     if (cp1 != NULL)    {
  338.                         int len = (int) (cp1 - cp);
  339.                         if ((thisword->hname = calloc(1, len)) != NULL)    
  340.                             strncpy(thisword->hname, cp+1, len-1);
  341.                         memmove(cp, cp1+1, strlen(cp1));
  342.                     }
  343.                 }
  344.             }
  345.         }
  346.         PutItemText(wnd, ID_HELPTEXT, hline);
  347.         if (++linectr == ClientHeight(cwnd))
  348.             SendMessage(cwnd, PAINT, 0, 0);
  349.     }
  350. }
  351.  
  352. static int HelpLength(char *s)
  353. {
  354.     int len = strlen(s);
  355.     char *cp = strchr(s, '[');
  356.     while (cp != NULL)    {
  357.         len -= 4;
  358.         cp = strchr(cp+1, '[');
  359.     }
  360.     cp = strchr(s, '<');
  361.     while (cp != NULL)    {
  362.         char *cp1 = strchr(cp, '>');
  363.         if (cp1 != NULL)
  364.             len -= (int) (cp1-cp)+1;
  365.         cp = strchr(cp1, '<');
  366.     }
  367.     return len;
  368. }
  369.  
  370. /* ----------- load the help text file ------------ */
  371. void LoadHelpFile()
  372. {
  373.     char *cp;
  374.  
  375.     if (Helping)
  376.         return;
  377.     UnLoadHelpFile();
  378.     if ((helpfp = OpenHelpFile()) == NULL)
  379.         return;
  380.     *hline = '\0';
  381.     while (*hline != '<')    {
  382.         if (fgetshelp() == NULL)    {
  383.             fclose(helpfp);
  384.                return;
  385.         }
  386.     }
  387.     while (*hline == '<')   {
  388.         if (strncmp(hline, "<end>", 5) == 0)
  389.             break;
  390.  
  391.         if ((ThisHelp = calloc(1, sizeof(struct helps))) == NULL)
  392.             break;
  393.  
  394.         if (FirstHelp == NULL)
  395.             FirstHelp = ThisHelp;
  396.  
  397.         /* -------- parse the help window's text name ----- */
  398.         if ((cp = strchr(hline, '>')) == NULL)
  399.             continue;
  400.         *cp = '\0';
  401.         if ((ThisHelp->hname=malloc(strlen(hline+1)+1))==NULL)
  402.             break;
  403.         strcpy(ThisHelp->hname, hline+1);
  404.  
  405.         HelpFilePosition(&ThisHelp->hptr, &ThisHelp->bit);
  406.  
  407.         if (fgetshelp() == NULL)
  408.             break;
  409.  
  410.         /* ------- build the help linked list entry --- */
  411.         while (*hline == '[')    {
  412.             HelpFilePosition(&ThisHelp->hptr, &ThisHelp->bit);
  413.             /* ------ parse the <<prev button pointer ------- */
  414.             if (strncmp(hline, "[<<]", 4) == 0)    {
  415.                 char *cp = strchr(hline+4, '<');
  416.                 if (cp != NULL)    {
  417.                     char *cp1 = strchr(cp, '>');
  418.                     if (cp1 != NULL)    {
  419.                         int len = (int) (cp1-cp);
  420.                         ThisHelp->PrevName = calloc(1, len);
  421.                         if (ThisHelp->PrevName != NULL)
  422.                             strncpy(ThisHelp->PrevName, cp+1, len-1);
  423.                     }
  424.                 }
  425.                 if (fgetshelp() == NULL)
  426.                     break;
  427.                 continue;
  428.             }
  429.             /* ------ parse the next>> button pointer ------- */
  430.             else if (strncmp(hline, "[>>]", 4) == 0)    {
  431.                 char *cp = strchr(hline+4, '<');
  432.                 if (cp != NULL)    {
  433.                     char *cp1 = strchr(cp, '>');
  434.                     if (cp1 != NULL)    {
  435.                         int len = (int) (cp1-cp);
  436.                         ThisHelp->NextName = calloc(1, len);
  437.                         if (ThisHelp->NextName != NULL)
  438.                             strncpy(ThisHelp->NextName, cp+1, len-1);
  439.                     }
  440.                 }
  441.                 if (fgetshelp() == NULL)
  442.                     break;
  443.                 continue;
  444.             }
  445.             else
  446.                 break;
  447.         }
  448.         ThisHelp->hheight = 0;
  449.         ThisHelp->hwidth = 0;
  450.         ThisHelp->NextHelp = NULL;
  451.  
  452.         /* ------ append entry to the linked list ------ */
  453.         if (LastHelp != NULL)
  454.             LastHelp->NextHelp = ThisHelp;
  455.         LastHelp = ThisHelp;
  456.  
  457.         /* -------- move to the next <> token ------ */
  458.         if (fgetshelp() == NULL)
  459.             strcpy(hline, "<end>");
  460.         while (*hline != '<')    {
  461.             ThisHelp->hwidth =
  462.                 max(ThisHelp->hwidth, HelpLength(hline));
  463.             ThisHelp->hheight++;
  464.             if (fgetshelp() == NULL)
  465.                 strcpy(hline, "<end>");
  466.         }
  467.     }
  468.     fclose(helpfp);
  469. }
  470.  
  471. /* ------ free the memory used by the help file table ------ */
  472. void UnLoadHelpFile(void)
  473. {
  474.     while (FirstHelp != NULL)    {
  475.         ThisHelp = FirstHelp;
  476.         if (ThisHelp->hname != NULL)
  477.             free(ThisHelp->hname);
  478.         if (ThisHelp->PrevName != NULL)
  479.             free(ThisHelp->PrevName);
  480.         if (ThisHelp->NextName != NULL)
  481.             free(ThisHelp->NextName);
  482.         FirstHelp = ThisHelp->NextHelp;
  483.         free(ThisHelp);
  484.     }
  485.     ThisHelp = LastHelp = NULL;
  486. }
  487.  
  488. /* ------------ display help text ----------- */
  489. int DisplayHelp(WINDOW wnd, char *Help)
  490. {
  491.     if (Helping)
  492.         return TRUE;
  493.     FindHelp(Help);
  494.     if (ThisHelp != NULL)    {
  495.         if (LastStack == NULL || stricmp(Help, LastStack->hname))    {
  496.             ThisStack = calloc(1,sizeof(struct HelpStack));
  497.             if (ThisStack != NULL)    {
  498.                 ThisStack->hname = malloc(strlen(Help)+1);
  499.                 if (ThisStack->hname != NULL)
  500.                     strcpy(ThisStack->hname, Help);
  501.                 ThisStack->PrevStack = LastStack;
  502.                 LastStack = ThisStack;
  503.             }
  504.         }
  505.         if ((helpfp = OpenHelpFile()) != NULL)    {
  506.             DBOX *db;
  507.             int offset, i;
  508.  
  509.             if ((db = calloc(1,sizeof HelpBox)) != NULL)    {
  510.                 memcpy(db, &HelpBox, sizeof HelpBox);
  511.                 SeekHelpLine(ThisHelp->hptr, ThisHelp->bit);
  512.                    fgetshelp();
  513.                 hline[strlen(hline)-1] = '\0';
  514.                 if ((db->dwnd.title = malloc(strlen(hline)+1)) != NULL)
  515.                     strcpy(db->dwnd.title, hline);
  516.                 db->dwnd.h = min(ThisHelp->hheight, MAXHEIGHT)+7;
  517.                 db->dwnd.w = max(45, ThisHelp->hwidth+6);
  518.                 BestFit(wnd, &db->dwnd);
  519.                 db->ctl[0].dwnd.w = max(40, ThisHelp->hwidth+2);
  520.                 db->ctl[0].dwnd.h = min(ThisHelp->hheight, MAXHEIGHT)+2;
  521.                 offset = (db->dwnd.w-40) / 2;
  522.                 for (i = 1; i < 5; i++)    {
  523.                     db->ctl[i].dwnd.y = min(ThisHelp->hheight, MAXHEIGHT)+3;
  524.                     db->ctl[i].dwnd.x += offset;
  525.                 }
  526.                 if (ThisStack != NULL)
  527.                     if (ThisStack->PrevStack == NULL)
  528.                         DisableButton(db, ID_BACK);
  529.                 if (ThisHelp->NextName == NULL)
  530.                     DisableButton(db, ID_NEXT);
  531.                 if (ThisHelp->PrevName == NULL)
  532.                     DisableButton(db, ID_PREV);
  533.                 DialogBox(wnd, db, TRUE, HelpBoxProc);
  534.             }
  535.             fclose(helpfp);
  536.             return TRUE;
  537.         }
  538.     }
  539.     return FALSE;
  540. }
  541.  
  542. static void DisplayDefinition(WINDOW wnd, char *def)
  543. {
  544.     WINDOW dwnd;
  545.     WINDOW hwnd = wnd;
  546.     int y;
  547.  
  548.     if (GetClass(wnd) == POPDOWNMENU)
  549.         hwnd = GetParent(wnd);
  550.     y = GetClass(hwnd) == MENUBAR ? 2 : 1;
  551.     FindHelp(def);
  552.     if (ThisHelp != NULL)    {
  553.         clearBIOSbuffer();
  554.         if ((helpfp = OpenHelpFile()) != NULL)    {
  555.             clearBIOSbuffer();
  556.             dwnd = CreateWindow(TEXTBOX,
  557.                                NULL,
  558.                                GetClientLeft(hwnd),
  559.                                GetClientTop(hwnd)+y,
  560.                                min(ThisHelp->hheight, MAXHEIGHT)+3,
  561.                                ThisHelp->hwidth+2,
  562.                                NULL,
  563.                                wnd,
  564.                                NULL,
  565.                                HASBORDER | NOCLIP | SAVESELF);
  566.             if (dwnd != NULLWND)    {
  567.                 clearBIOSbuffer();
  568.                 /* ----- read the help text ------- */
  569.                 SeekHelpLine(ThisHelp->hptr, ThisHelp->bit);
  570.                 while (TRUE)    {
  571.                     clearBIOSbuffer();
  572.                     if (fgetshelp() == NULL)
  573.                         break;
  574.                     if (*hline == '<')
  575.                         break;
  576.                     hline[strlen(hline)-1] = '\0';
  577.                     SendMessage(dwnd, ADDTEXT, (PARAM) hline, 0);
  578.                 }
  579.                 SendMessage(dwnd, SHOW_WINDOW, 0, 0);
  580.                 SendMessage(NULLWND, WAITKEYBOARD, 0, 0);
  581.                 SendMessage(NULLWND, WAITMOUSE, 0, 0);
  582.                 SendMessage(dwnd, CLOSE_WINDOW, 0, 0);
  583.             }
  584.             fclose(helpfp);
  585.         }
  586.     }
  587. }
  588.  
  589. static int wildcmp(char *s1, char *s2)
  590. {
  591.     while (*s1 || *s2)    {
  592.         if (tolower(*s1) != tolower(*s2))
  593.             if (*s1 != '?' && *s2 != '?')
  594.                 return 1;
  595.         s1++, s2++;
  596.     }
  597.     return 0;
  598. }
  599.  
  600. static void FindHelp(char *Help)
  601. {
  602.     ThisHelp = FirstHelp;
  603.     while (ThisHelp != NULL)    {
  604.         if (wildcmp(Help, ThisHelp->hname) == 0)
  605.             break;
  606.         ThisHelp = ThisHelp->NextHelp;
  607.     }
  608. }
  609.  
  610. static void FindHelpWindow(WINDOW wnd)
  611. {
  612.     ThisHelp = FirstHelp;
  613.     while (ThisHelp != NULL)    {
  614.         if (wnd == ThisHelp->hwnd)
  615.             break;
  616.         ThisHelp = ThisHelp->NextHelp;
  617.     }
  618. }
  619.  
  620. static int OverLap(int a, int b)
  621. {
  622.     int ov = a - b;
  623.     if (ov < 0)
  624.         ov = 0;
  625.     return ov;
  626. }
  627.  
  628. /* ------ compute the best location for a dialog box ----- */
  629. static void BestFit(WINDOW wnd, DIALOGWINDOW *dwnd)
  630. {
  631.     int above, below, right, left;
  632.     if (GetClass(wnd) == MENUBAR || GetClass(wnd) == APPLICATION)    {
  633.         dwnd->x = dwnd->y = -1;
  634.         return;
  635.     }
  636.     /* --- compute above overlap ---- */
  637.     above = OverLap(dwnd->h, GetTop(wnd));
  638.     /* --- compute below overlap ---- */
  639.     below = OverLap(GetBottom(wnd), SCREENHEIGHT-dwnd->h);
  640.     /* --- compute right overlap ---- */
  641.     right = OverLap(GetRight(wnd), SCREENWIDTH-dwnd->w);
  642.     /* --- compute left  overlap ---- */
  643.     left = OverLap(dwnd->w, GetLeft(wnd));
  644.  
  645.     if (above < below)
  646.         dwnd->y = max(0, GetTop(wnd)-dwnd->h-2);
  647.     else
  648.         dwnd->y = min(SCREENHEIGHT-dwnd->h, GetBottom(wnd)+2);
  649.     if (right < left)
  650.         dwnd->x = min(GetRight(wnd)+2, SCREENWIDTH-dwnd->w);
  651.     else
  652.         dwnd->x = max(0, GetLeft(wnd)-dwnd->w-2);
  653.  
  654.     if (dwnd->x == GetRight(wnd)+2 || dwnd->x == GetLeft(wnd)-dwnd->w-2)
  655.         dwnd->y = -1;
  656.     if (dwnd->y ==GetTop(wnd)-dwnd->h-2 || dwnd->y == GetBottom(wnd)+2)
  657.         dwnd->x = -1;
  658. }
  659.  
  660. #endif
  661.